package cn.tmpan.project;

import cn.tmpan.project.interceptor.CommonInterceptor;
import cn.tmpan.project.interceptor.ErrorPageInterceptor;
import cn.tmpan.project.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * The type Web mvc config.
 *
 * @Author tieMinPan
 * @Desc: Description
 * @Date 2019 -05-07 16:01
 */
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private ErrorPageInterceptor errorPageInterceptor;

    @Autowired
    private CommonInterceptor commonInterceptor;


    /**
     * 功能描述:
     * 配置静态资源,避免静态资源请求被拦截
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * swagger 配置
         */
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**")
                .addResourceLocations("classpath:/templates/");


    }

    /**
     * 拦截URL
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         *@desc 跨域处理
         */
        registry.addInterceptor(commonInterceptor)
                .addPathPatterns("/**");

        registry.addInterceptor(loginInterceptor)
                /**
                 *@desc 用于添加拦截路径规则
                 */
                .addPathPatterns("/admin/**")
                /**
                 *@desc 排除管理员登录界面以及接口
                 */
                .excludePathPatterns("/admin/login/**");
        /**
         *@desc 拦截错误页面
         */
        registry.addInterceptor(errorPageInterceptor)
                .addPathPatterns("/**");

    }


}